$(function() {
	/* 项目路径 */
	var baseURL = /knowledge/
	/* 构建列表 */
	$("#jqGrid").jqGrid(
					{
						url : baseURL + '/document/selectKwByUserOrDept',
						datatype : "json",
						colModel : [
								/*{
									label : '编号',
									name : 'documentId',
									index : "documentOrigin",
									width : 20,
									key : true
								},*/
								{label : '来源',name : 'documentOrigin',index : "documentOrigin",width : 45},
								{label : '作者',name : 'documentAuthor',width : 55},
								{label : '时间',name : 'documentUploadDate',width : 60},
								{label : '摘要',name : 'documentAbstract',width : 100},
								{label : '备注',name : 'documentMarks',width : 100},
								{label : '关键词',name : 'documentKeyword',width : 100},
/*								{
									label : '审批状态',
									name : 'documentState',
									width : 42,
									formatter : function(value, options, row) {

										if (value == 0) {
											return '<span class="label label-info">等待审核</span>';
										}
										if (value == 1) {
											return '<span class="label label-success">审核通过</span>';
										}
										if (value == 2) {
											return '<span class="label label-danger">审核不过</span>';
										}
									}
								},*/
								{label : '标签',name : 'lableInfos',width : 100,
									formatter : function(value, options, row) {
										debugger
										var lableName="";
					                    for (var i = 0; i < value.length; i++) {
					                    	lableName+=value[i].lableName+",";
					                    }
										return lableName;
									}
								},
					            {
					                label: '文档', name: 'files', width: 100, formatter: function (value, options, row) {
					                    var spanItem = '<div id="aitemDiv">'
					                    	
					                    //if(value.length>1){
					                    //	spanItem= '<div style="height:20px;overflow:hidden;">'
					                   // }<a style="display:block;height:20px;width:20px;background:url(../images/down.png) ;background-repeat:no-repeat;background-size:contain;"></a>
					                    for (var i = 0; i < value.length; i++) {
					                        debugger
											console.log(value[i].documentSaveurl)
											var url = value[i].documentSaveurl
											
											var inputFile=url
											var newUrl = url.replace(/\\/g,"\/");
											var realUrl = baseURL + "localFile"+ newUrl.substr(12)
											var aItm=null;
											var index = realUrl.lastIndexOf(".");
											var ext = realUrl.substr(index+1).toLowerCase();
											var fileName=inputFile.split("\\")[3].substr(15)
											realUrl = encodeURI(realUrl)
											
											inputFile=encodeURI(inputFile)
											if(ext=="pdf"||ext=="txt"){//pdf txt 直接利用浏览器预览
												aItm = "<a class='label label-info'  style='display:block;margin-bottom:2px;'  target='_blank' href="+ realUrl + " title="+fileName+" >"+fileName+" </a>"
											}else{
												var flag=function(ext){
													if(ext=="pdf"||ext=="doc"||ext=="docx"||ext=="ppt"||ext=="pptx"||ext=="xlsx"||ext=="xls"){
														return true;
													}else{
														return false;
													}
												}
												if(flag(ext)){
													aItm = "<a class='label label-info' style='display:block;margin-bottom:2px;' documentId="+row.documentId+" inputFile="+inputFile+" id='officetopdf' title="+fileName+" >"+fileName+"</a>"
												}else{
													aItm = "<a class='label label-info' style='display:block;margin-bottom:2px;' documentId="+row.documentId+" inputFile="+inputFile+" id='downLoadFile' title="+fileName+" >"+fileName+"</a>"
												}

											}
											spanItem += aItm
					                    }
					                    spanItem + '</div>'
					                    return spanItem;
					                }
					            } 
								],
						viewrecords : true,
						height : 385,
						rowNum : 10,
						rowList : [ 10, 30, 50 ],
						/*rownumbers : true,
						rownumWidth : 20,*/
						autowidth : true,
						/*multiselect : true,*/
						pager : "#jqGridPager",
						jsonReader : {
							root : "page.list",
							page : "page.pageNum",
							total : "page.pages",
							records : "page.total"
						},
						prmNames : {
							page : "page",
							rows : "limit",
							order : "order"
						},
						gridComplete : function() {
							// 隐藏grid底部滚动条
							$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x" : "hidden"});
							
				            //修改样式 start ==========================
				            var divs=$("td").filter("[role=gridcell]").find("div")
				            for(var i=0;i<divs.length;i++){
				            	//console.log("itmes: "+divs[i].childElementCount)
				            	lineCount=divs[i].childElementCount;
				            	if(lineCount>1){
				            		var tds=$(divs[i].parentElement.parentElement).find("td")
				            		for(var j=0;j<tds.length-1;j++){
				            			debugger
				            			$(tds[j]).css({"padding-top":""+lineCount+"0px"})
				            		}
				            	}
				            }
				            //修改样式 end ==========================
				            vm.getAllLable();
						}
					});
    $(document).on("click","a[id='officetopdf']",function(){
    	var athis=$(this)
    	var documentId=parseInt(athis.attr("documentId"))
    	var inputFile=athis.attr("inputFile")
    	var url=baseURL+"lookdocument/officeToPdf"
    	debugger
    	var data={"inputFile":inputFile,"documentId":documentId}
    	var newwindow=window.open("about:blank");
    	$.post(url,data,function(r){
    		debugger;
    		console.log(r)
    		var url=r.url
    		var newUrl = url.replace(/\\/g,"\/");
			var realUrl = baseURL + "localFile"+ newUrl.substr(17)
			newwindow.location.href=realUrl;
            newwindow.focus();
    	});
    })
})
var vm = new Vue({
	el : '#rrapp',
	data : {
		q : {
			origin:null,
			author:null,
			abstract:null,
			marks:null,
			keyword:null
		},
		showList : true,
		showUserList:true,
		showDeptList:true,
		title : null,
		lableList:[],
		document : {
			documentUploadDate : null,
			documentAbstract : null,
			documentId : null,
		},
		userList : [],
		deptList : [],
		userSelected : null,
		deptSelected : null
	},
	methods : {
		query : function() {
			//origin,author,abstract,marks,keyword
/*			if (vm.q.origin ==null&&
				vm.q.author ==null&&
				vm.q.abstract ==null&&
				vm.q.marks ==null&&
				vm.q.keyword ==null
			){
				
			}*/
			vm.reload();
		},
		reload : function() {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData : {
					'origin' : vm.q.origin,
					'author' : vm.q.author,
					'abstract' : vm.q.abstract,
					'marks' : vm.q.marks,
					'keyword' : vm.q.keyword,
					'lableid':vm.q.documentLable
				},
				page : page
			}).trigger("reloadGrid");
		},
		preReleaseToDept : function() {
			var documentId = getSelectedRow();
			if (documentId == null) {
				return;
			}
			vm.showList = false;
			vm.showDeptList = false;
			vm.showUserList = true;
			vm.title = "文档发布";
			// 获取角色信息
			this.getUserAndDept();
			vm.document.documentId=documentId
		},
		preReleaseToUser : function() {
			var documentId = getSelectedRow();
			if (documentId == null) {
				return;
			}
			vm.showList = false;
			vm.showDeptList = true;
			vm.showUserList = false;
			vm.title = "文档发布";
			// 获取角色信息
			this.getUserAndDept();
			vm.document.documentId=documentId
		},
		release : function() {
			console.log("vm.document.documentId   :"+vm.document.documentId)
			if (isBlank(vm.userSelected) && isBlank(vm.deptSelected)) {
				alert("发布部门或人不能为空");
				return true;
			}
			var msg = null;
			var userId, userName, deptId, deptName;
			alert(vm.document.documentId)
			if (!isBlank(vm.userSelected)) {
				userId = vm.userSelected.split("_")[0]
				userName = vm.userSelected.split("_")[1]
				msg = "确定发布给【" + userName + "】 么？";
			}
			if (!isBlank(vm.deptSelected)) {
				deptId = vm.deptSelected.split("_")[0]
				deptName = vm.deptSelected.split("_")[1]
				msg = "确定发布给【" + deptName + "】 么？";
			}
			if (!isBlank(vm.deptSelected) && !isBlank(vm.userSelected)) {
				userId = vm.userSelected.split("_")[0]
				userName = vm.userSelected.split("_")[1]
				deptId = vm.deptSelected.split("_")[0]
				deptName = vm.deptSelected.split("_")[1]
				msg = "确定发布给【 " + deptName + " 】与【  " + userName + "】 么？";
			}
			
			confirm(msg, function() {
				$.post(baseURL + "/document/documentRelease", {
					"userId" : userId,
					"userName":userName,
					"deptId" : deptId,
					"deptName":deptName,
					"documentId" : vm.document.documentId
				}, function(result) {
					debugger
					if(result.code==500){
						alert(result.msg);
					}else{
						alert("发布成功!");
					}
					vm.deptSelected=null;
					vm.userSelected=null;
					vm.reload();
				});

			});

		},
		getUserAndDept : function() {
			$.get(baseURL + "document/getUserAndDept", function(r) {
				vm.userList = r.userDept.userList
				vm.deptList = r.userDept.deptList
				debugger
			});
		},
		passOrFalse : function() {
			if (event) {
				var status = event.target.getAttribute("status")
				debugger
			}
			var msg = ""
			if (status == 1) {
				msg = '确定要审核通过？'
			} else {
				msg = '确定要审核不过？'
			}
			confirm(msg, function() {
				$.post(baseURL + "/document/passOrfailService", {
					"documentid" : vm.document.documentId,
					"status" : status
				}, function(result) {
					alert("操作成功");
					vm.reload();
				});
			});
		},
		officetopdf:function(){
			alert(11111)
		},
        getAllLable:function(){
        	$.get(baseURL+"/label/getAllLable",function(r){
        		vm.lableList=r.lableList;
        		console.log(r.lableList)
        	})
        }
	}
});